package com.wephone.Student.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.wephone.Student.R;

import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.GroupInfoDetailResponse;
import com.wephone.Student.bean.GroupResponse;
import com.wephone.Student.bean.GroupsListResponse;
import com.wephone.Student.bean.data.GroupInfo;
import com.wephone.Student.bean.data.MyLocationInfo;
import com.wephone.Student.bean.data.UserInfoData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.RoundImageView;
import com.wephone.Student.util.Util;


public class MyGroupsActivity extends BaseActivity {
	private String uid;
	private TextView top_bar_title;
	private ArrayList<GroupInfo> groups = new ArrayList<GroupInfo>();//群列表
	private Button add_group;
	private ListView group_list;
	private GroupViewAdapter adapter;
	private int stage;
	
	//获取详情
	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(MyGroupsActivity.this,GroupsListResponse.class){
			@Override
			public void onSuccess(String result) {
				findViewById(R.id.progressBar).setVisibility(View.GONE);
				super.onSuccess(result);
				String pre="";
				if(uid.equals( BaseActivity.getMyInfo(getApplicationContext()).getuId())){
					pre="我";
				}
				String groupName = "";
				switch (stage) {
				case 1:
					groupName = pre+"管理的群组("+0+")";
					break;
				case 2:
					groupName = pre+"加入的群组("+0+")";
					break;
				case 3:
					groupName = "正在申请中的群组("+0+")";
					break;
				default:
					break;
				}
				GroupInfo groupInfo = new GroupInfo();
				groupInfo.setGroupid("0");
				groupInfo.setName(groupName);
				groups.add(groupInfo);
				if(getDataSuccess){
					List<GroupInfo> data = ((GroupsListResponse)response).getData();
					int state=0;
					switch (stage) {
					case 1:
						groupInfo.setName(pre+"管理的群组("+data.size()+")");
						state = 1;
						break;
					case 2:
						groupInfo.setName(pre+"加入的群组("+data.size()+")");
						state = 2;
						break;
					case 3:
						groupInfo.setName(pre+"正在申请中的群组("+data.size()+")");
						state = 3;
						break;
					default:
						break;
					}
					int length = data.size();
					for(int i=0;i<length;i++){
						GroupInfo temGroupInfo = data.get(i);
						temGroupInfo.setStatu(state+"");
						groups.add(temGroupInfo);
						
					}
					
				}
				adapter.notifyDataSetChanged();
				if(stage == 1){
					RequestParams params2 = new RequestParams();
					params2.put("uId", uid);
					HttpClient.getInstance().get(Constants.API_GET_JOIN_CHART, params2, getHandler());
				}else if(stage == 2){
					RequestParams params2 = new RequestParams();
					params2.put("uId", uid);
					HttpClient.getInstance().get(Constants.API_GET_APPLY_CHART, params2, getHandler());
				}
				stage++;
			}
		};
		return handler;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_groups);
		
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			uid = bundle.getString("uid");
			init();
		}else{
			finish();
			Toast.makeText(getApplicationContext(), "数据异常", Toast.LENGTH_SHORT).show();
		}
	}
	
	private void init(){
		
		top_bar_title = (TextView)findViewById(R.id.top_bar_title);
		add_group = (Button)findViewById(R.id.add_group);
		add_group.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				startActivity(new Intent(getApplicationContext(),NewGroupActivity1.class));
			}
		});
		if(uid.equals(BaseActivity.getMyInfo(getApplicationContext()).getuId())){
			top_bar_title.setText("我的群组");
			
		}else{
			top_bar_title.setText(("个人群组"));
			add_group.setVisibility(View.GONE);
		}
		
		group_list = (ListView)findViewById(R.id.group_list);
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		adapter = new GroupViewAdapter();
		group_list.setAdapter(adapter);
		group_list.setOnItemClickListener(new ListView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Bundle bundle = new Bundle();
				GroupInfo groupInfo = groups.get(arg2);
				if(groupInfo.getGroupid().equals("0")){
				}else{
					bundle.putSerializable("groupData", groups.get(arg2));
					startActivity(new Intent(getApplicationContext(),GroupInfoDetailActivity.class).putExtras(bundle));
				}
			}});
		
			//refresh();
	}	
	
	private void refresh(){
		findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
		groups.clear();
		stage = 1;
		RequestParams params2 = new RequestParams();
		params2.put("uId", uid);
		HttpClient.getInstance().get(Constants.API_GET_CREATE_CHART, params2, getHandler());
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		refresh();
	}
	private DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.default_user_logo)
    .showImageOnFail(R.drawable.default_user_logo)
    .bitmapConfig(Bitmap.Config.ARGB_8888)
    .cacheInMemory()
    .cacheOnDisc()
//    .displayer(new RoundedBitmapDisplayer(1000))
    .build();
	
	private class GroupViewAdapter extends BaseAdapter{
		private static final int TYPE_ITEM = 0;
		private static final int TYPE_SEPARATOR = 1;
		
		@Override
		public int getCount() {
			return groups.size();
		}

		@Override
		public int getItemViewType(int position) {
			GroupInfo groupInfo = groups.get(position);
			if(groupInfo.getGroupid().equals("0")){
				return TYPE_SEPARATOR;
			}else{
				return TYPE_ITEM;
			}
		}

		@Override
		public int getViewTypeCount() {
			return 2;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			int rowType = getItemViewType(position);
			if(rowType == TYPE_ITEM){
					if(convertView==null){
						convertView = getLayoutInflater().inflate(R.layout.include_goups_list_item, null);
					}
					GroupInfo userData = groups.get(position);
					
					TextView itemName = (TextView)convertView.findViewById(R.id.item_name);
					itemName.setText(userData.getName());
					TextView itemSign = (TextView)convertView.findViewById(R.id.item_signature);
					itemSign.setText("群介绍:"+userData.getContent());
					
					TextView itemNum = (TextView)convertView.findViewById(R.id.item_num);
					itemNum.setText(userData.getNownum()+"/"+userData.getMaxnum());
					
					TextView itemLoc = (TextView)convertView.findViewById(R.id.item_loc);
					itemLoc.setText(userData.getClog());
					if(userData.getStatu().equals("1")){
						convertView.findViewById(R.id.item_status).setVisibility(View.GONE);
					}else{
						convertView.findViewById(R.id.item_status).setVisibility(View.GONE);
					}
					
					RoundImageView itemImage = (RoundImageView)convertView.findViewById(R.id.item_image);
					if(!TextUtils.isEmpty(userData.getPic())){
						imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getPic(), itemImage, options);
					}else{
						itemImage.setImageResource(R.drawable.default_user_logo);
					}
			}else{
				if(convertView==null){
					convertView = getLayoutInflater().inflate(R.layout.include_group_member_header, null);
				}
				GroupInfo userData = groups.get(position);
				TextView groupName = (TextView)convertView.findViewById(R.id.groupName);
				groupName.setText(userData.getName());
				
				TextView groupNum = (TextView)convertView.findViewById(R.id.groupNum);
				groupNum.setVisibility(View.GONE);
			}
			return convertView;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return groups.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}
	}
}
